package com.ckjw.gobang.view;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.ckjw.gobang.connect.UserClientService;
import com.ckjw.gobang.data.Player;

/**
 * 登陆页面
 * @author lck and djw
 * @Date 2022年6月12日-下午3:03:33
 */
public class login {
    JFrame jf = new JFrame("登陆窗口");
    final int WIDES = 400;
    final int HIGHS = 300;
    /**
     * 组装视图
     */
    public void init() throws IOException {
        //获取当前的电脑屏幕的宽高
        double wide = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        double high = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        //设置窗口在电脑屏幕出现位置
        jf.setBounds((int) (wide - WIDES) / 2, (int) (high - HIGHS) / 2, WIDES, HIGHS);
        jf.setLayout(null);
        
        Box vbox = Box.createVerticalBox();
        vbox.setBounds(100,30,200,200);
        //第一行
        Box ubox = Box.createHorizontalBox();
        JLabel ulabel = new JLabel("账号:");
        JTextField ufield = new JTextField(15);
        ubox.add(ulabel);
        ubox.add(Box.createHorizontalStrut(20));
        ubox.add(ufield);
        
        //第二行
        Box pbox = Box.createHorizontalBox();
        JLabel plabel = new JLabel("密码:");
        JPasswordField pfield = new JPasswordField(15);
        pbox.add(plabel);
        pbox.add(Box.createHorizontalStrut(20));
        pbox.add(pfield);

        //第三行
        Box bbox = Box.createHorizontalBox();
        JButton ubutton = new JButton("登录");
        JButton rbutthon = new JButton("注册");
        bbox.add(ubutton);
        bbox.add(Box.createVerticalStrut(40));
        bbox.add(rbutthon);
        /**
         * 输入账号密码换行并登录的事件处理思路：
         * addAction.用来监听field和button
         * addAction 监听field时的对象是输入回车
         * addAction 监听button时的对象是鼠标点击按钮
         * 所以在field被addActon监听时，遇到回车就可以进行处理事件，
         * 分别是：pfield.requestFocus()
         * ubutton.doClick()
         */
        ufield.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               pfield.requestFocus();
            }
        });
        pfield.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ubutton.doClick();
            }
        });
        //登录
        /**
         * 监听登录按钮是否被点击
         */
        ubutton.addActionListener(event ->{
        	 String user = ufield.getText().trim();
             String passwd =  pfield.getText().trim();
             System.out.println(user+passwd);
             
             //判断是是否已有用户登陆
             if (Player.onlineuser==null) {
             //连接socket 发送用户名密码  接受用户名密码 验证是否登陆成功
             if (new UserClientService().checkUser(user, passwd)) {
				System.out.println("登陆成功");
				JOptionPane.showMessageDialog(MainInterface.mainInterface, "登陆成功", "提示消息",
						JOptionPane.INFORMATION_MESSAGE);
				
				//可以封装函数
				MyMenuBar.my.setVisible(false);
				
				jf.dispose();
				UserPanel.userPanel.setVisible(false);
				UserPanel2.userPanel.setVisible(true);
				//MainInterface.mainInterface.repaint();
			}
             else {
				System.err.println("登陆失败");
				JOptionPane.showMessageDialog(MainInterface.mainInterface, "登陆失败", "提示消息",
						JOptionPane.WARNING_MESSAGE);
				jf.dispose();
			}
           }
             
        });
        //注册
        rbutthon.addActionListener(event ->{
        	try {
				register.init();
				jf.dispose();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        });

        //装到垂直容器中
        vbox.add(Box.createVerticalStrut(50));
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(30));
        vbox.add(pbox);
        vbox.add(Box.createVerticalStrut(30));
        vbox.add(bbox);

        jf.add(vbox);
        jf.setResizable(false);
        jf.setVisible(true);
    }

}
